import random
import requests
import base64
from ddddocr import DdddOcr

ocr = DdddOcr(show_ad=False)
while True:
    yzm_url = f'https://www.labnetwork.com.cn/gateway/basic/mall/oauth/code?{random.random():.16f}'
    yzm_res = requests.get(yzm_url)
    img_url = yzm_res.json()['data']['img']
    uuid_code = yzm_res.json()['data']['uuid']
    img_content = base64.b64decode(img_url.split(',')[1])
    with open(f'./static/yzm/博览.jpg', 'wb') as f:
        f.write(img_content)
    r = ocr.classification(img_content)
    login_url = 'https://www.labnetwork.com.cn/gateway/basic/mall/oauth/login'
    login_res = requests.post(login_url, json={
        "userEmail": "2094729537@qq.com",
        "password": "Zdz123123",
        "captchaCode": f"{r}",
        "captchaKey": f"{uuid_code}"
    })
    if '验证码错误' in login_res.text:
        print('重新识别验证码')
    else:
        token = login_res.json()['data']['userInfo']['token']
        print(token)
        break
page_url = 'https://www.labnetwork.com.cn/gateway/oldapi/mall/chemical/products'
page_res = requests.post(page_url, json={
    "query": "五/六元芳香环->吡唑",
    "country": "CN",
    "pageSize": 10,
    "chemIds": [],
    "suppliers": [],
    "rank": 0,
    "packSize": "",
    "packUnit": "",
    "unitStock": "",
    "selectedUnit": "",
    "molecularWeightLow": "",
    "molecularWeightHigh": "",
    "locations": [],
    "startRecord": 10,
    "modelRange": [
        111.14,
        302.16
    ]
}, headers={
    'token': token,
})
print(page_res.json())
